1181B - Split a Number - CodeForces Solution


greedy implementation strings *1500

Please click on ads to support us..

Python Code:

n = int(input())
s = input()
mx = n
ans = 10**n
for i in range(0,n-1):
    if s[i+1]!='0':
        mx = min(mx,max(i+1,n-(i+1)))
for i in range(0,n-1):
    if(s[i+1]!='0' and max(i+1,n-(i+1))==mx):
        ans = min(ans,int(s[0:i+1])+int(s[i+1:n]))
print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
string stringgen(string a,string b)
{
    string res="";
    int carry=0,sum=0;
    string s="";
    if(a.length()<b.length())
    {
        for(int i=0;i<b.length()-a.length();i++)
        s+='0';
        a=s+a;
    }
    else if(a.length()>b.length())
    {
        for(int i=0;i<a.length()-b.length();i++)
        s+='0';
        b=s+b;
    }
    for(int i=a.length()-1;i>=0;i--)
    {
         int sum=(a[i]-'0'+b[i]-'0')+carry;
         carry=sum/10;
         res+=to_string(sum%10);
    }
    if(carry!=0)
    res+=to_string(carry);
    reverse(res.begin(),res.end());
    return res;
    
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
            int l,x,y;
            cin>>l;
            string s;
            cin>>s;
            string a,b;
            if(l%2==0)
            {
                x=l/2-1;
                y=l/2;
            }
            else
            {
                x=l/2-1;
                y=l/2+1;
            }
                for(int i=x;i>=0;i--)
                {
                    if(s[i]!='0')
                    {
                        if(i-1>=0)
                        {
                            //cout<<s.substr(0,i)<<" "<<s.substr(i,l-i)<<endl;
                            a=stringgen(s.substr(0,i),s.substr(i,l-i));
                            break;
                        }
                        else
                        {
                            a=stringgen("",s.substr(i,l-i));
                            break;
                        }
                    }
                }
                for(int i=y;i<l;i++)
                {
                    if(s[i]!='0')
                    {
                        if(i-1>=0)
                        {
                            b=stringgen(s.substr(0,i),s.substr(i,l-i));
                            break;
                        }
                        else
                        {
                            b=stringgen("",s.substr(i,l-i));
                            break;
                        }
                    }
                }
                if(b.length()==0)
                b=a;
                if(l%2==0)
                {
                    if(a.length()==b.length())
                        cout<<min(a,b)<<endl;
                   else if(a.length()<b.length())
                    cout<<a<<endl;
                   else
                    cout<<b<<endl;
                }
                else
                {
                    if(s[l/2]=='0')
                    {
                        if(a.length()==b.length())
                        cout<<min(a,b)<<endl;
                        else if(a.length()<b.length())
                        cout<<a<<endl;
                        else
                        cout<<b<<endl;
                    }
                    else
                    {
                        a=stringgen(s.substr(0,l/2 +1),s.substr(l/2+1,l/2));
                        b=stringgen(s.substr(0,l/2),s.substr(l/2,l/2 +1));
                        //cout<<b<<endl;
                        if(a.length()==b.length())
                        cout<<min(a,b)<<endl;
                       else if(a.length()<b.length())
                       cout<<a<<endl;
                      else
                       cout<<b<<endl;
                    }
                }
                
        
}


Comments

Submit
0 Comments
More Questions

1589C - Two Arrays
1510K - King's Task
126B - Password
462A - Appleman and Easy Task
839C - Journey
622A - Infinite Sequence
659C - Tanya and Toys
1266A - Competitive Programmer
234C - Weather
1332C - K-Complete Word
525C - Ilya and Sticks
1555C - Coin Rows
1324C - Frog Jumps
715A - Plus and Square Root
774D - Lie or Truth
1186D - Vus the Cossack and Numbers
505B - Mr Kitayuta's Colorful Graph
1324D - Pair of Topics
157B - Trace
34C - Page Numbers
279A - Point on Spiral
1294D - MEX maximizing
447A - DZY Loves Hash
23B - Party
63D - Dividing Island
1203E - Boxers
1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks